#帖子详情页
import time

from selenium.common import TimeoutException

from common.com import ForumDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys


class PostDetail:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = ForumDriver.driver


    def PostDetailTest(self):
        # 点击C++板块
        self.driver.find_element( By.CSS_SELECTOR, "#topBoardList > li:nth-child(3) > a" ).click()
        #等待帖子加载，点击第一个c++帖子
        WebDriverWait( self.driver, 3).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "#artical-items-body > div:nth-child(1) > div > div.col") ))
        self.driver.find_element( By.CSS_SELECTOR,"#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong" ).click()
        # 进入帖子详情页面
        # 检查元素
        self.driver.find_element( By.CSS_SELECTOR, "#article_details_author_avatar" )#用户头像
        self.driver.find_element( By.CSS_SELECTOR, "#details_article_content_title" )  #帖子标题
        self.driver.find_element( By.CSS_SELECTOR, "#details_article_content > p" )  # 内容
        self.driver.find_element( By.CSS_SELECTOR, "#article_details_author_name" )  # 作者昵称
        self.driver.find_element( By.CSS_SELECTOR, "#details_btn_like_count" )  # 点赞按钮
        self.driver.find_element( By.CSS_SELECTOR, "#details_artile_edit > span" )  # 编辑按钮
        self.driver.find_element( By.CSS_SELECTOR, "#details_btn_article_reply" )  # 回复按钮
        self.driver.find_element( By.CSS_SELECTOR,"#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll" )  # 回复框
        ForumDriver.getScreenShot()  # 截图
        # 点赞
        self.driver.find_element( By.CSS_SELECTOR, "#details_btn_like_count" ).click()
        ForumDriver.getScreenShot()
        #将网页滚动到页面的最底部
        self.driver.execute_script( "window.scrollTo(0,document.body.scrollHeight)" )
        # 最新回复里不编辑内容直接点击回复
        reply_btn = WebDriverWait( self.driver, 10 ).until(
            EC.element_to_be_clickable( (By.CSS_SELECTOR, "#details_btn_article_reply") )
        )
        self.driver.execute_script( "arguments[0].scrollIntoView();", reply_btn )
        # 等待一小段时间确保页面稳定
        time.sleep( 0.5)
        reply_btn.click()
        print( "已回复发布按钮" )
        #获取错误弹窗
        alert = WebDriverWait( self.driver, 3 ).until(
            EC.visibility_of_element_located( (By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")) )
        # 获取弹窗文本并打印
        alert_text = alert.text
        print( f"弹窗内容: {alert_text}" )
        ForumDriver.getScreenShot()  ## 屏幕截图

        # 评论发布：模拟键盘输入模式，try用于页面交互的 “不确定性” 处理
        try:
            # 等待回复框可点击（定位 CodeMirror 编辑区，需根据实际页面调整选择器）
            reply_editor = WebDriverWait( self.driver, 10 ).until(
                EC.element_to_be_clickable(
                    (By.CSS_SELECTOR, "#article_details_reply > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div")  # 关键：定位可编辑的 CodeMirror 区域
                )
            )
            # 初始化 ActionChains，模拟用户操作
            actions = ActionChains( self.driver )
            # 点击编辑区 → 激活输入
            actions.click( reply_editor ).perform()
            #输入评论内容（如 "666"）
            actions.send_keys( "666" ).perform()
            ForumDriver.getScreenShot()
            # 点击「回复」按钮（也可继续用 ActionChains 保持连贯性）
            reply_button = self.driver.find_element( By.CSS_SELECTOR, "#details_btn_article_reply" )
            actions.click( reply_button ).perform()
            ForumDriver.getScreenShot()
            print("成功点击回复按钮")
            # 等待回复成功提示
            WebDriverWait( self.driver, 10 ).until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")
                )
            )
            ForumDriver.getScreenShot()
        except TimeoutException:
            print( "回复操作超时：编辑区未加载/发布失败" )
        # 回到顶部
        self.driver.execute_script( "var q=document.documentElement.scrollTop=0" )

